package logic

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"user/databases"
	"user/model"

	"user/internal/svc"
	"user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteUserStarsGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeleteUserStarsGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserStarsGoodLogic {
	return &DeleteUserStarsGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DeleteUserStarsGoodLogic) DeleteUserStarsGood(in *user.DeleteUserStarGoodsRequest) (*user.DeleteUserStarGoodsResponse, error) {
	// todo: add your logic here and delete this line
	if len(in.IDs) == 0 {
		return &user.DeleteUserStarGoodsResponse{}, status.Errorf(codes.InvalidArgument, "ids is required")
	}

	// TODO:赋值给查训操作

	infos := []*model.UserStarGood{}
	err := databases.DB.Where("id in (?)", in.IDs).Find(&infos).Error
	if err != nil {
		return nil, err
	}

	err = databases.DB.Where("id in (?)", in.IDs).Delete([]*model.UserStarGood{}).Error
	if err != nil {
		return nil, err
	}

	var resInfos = []*user.UserStartGood{}
	for _, val := range infos {
		resInfos = append(resInfos, &user.UserStartGood{
			ID:        int64(val.ID),
			GoodSKUID: val.GoodSKUID,
			UserID:    val.UserID,
		})
	}
	return &user.DeleteUserStarGoodsResponse{
		Infos: resInfos,
	}, nil
}
